package com.ruoyi.base.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.base.domain.BaseBsProject;
import com.ruoyi.base.domain.vo.BdTreeVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.base.domain.vo.BaseBsBdVo;
import com.ruoyi.base.domain.bo.BaseBsBdBo;
import com.ruoyi.base.service.IBaseBsBdService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 标段
 *
 * @author zxs
 * @date 2024-09-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/base/bsBd")
public class BaseBsBdController extends BaseController {

    private final IBaseBsBdService iBaseBsBdService;

    /**
     * 查询标段列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BaseBsBdVo> list(BaseBsBdBo bo, PageQuery pageQuery) {
        return iBaseBsBdService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询标段、站点树列表
     */
    @SaCheckLogin
    @GetMapping("/bdTreeList")
    public R<List<BdTreeVo>> treeList(BaseBsBdBo bo) {
        return iBaseBsBdService.bdTreeList(bo);
    }

    /**
     * 查询标段、站点、供应商树列表
     */
    @SaCheckLogin
    @GetMapping("/supplierTreeList")
    public R<List<BdTreeVo>> supplierTreeList(BaseBsBdBo bo) {
        return iBaseBsBdService.supplierTreeList(bo);
    }

    /**
     * 查询标段、期数树列表
     */
    @SaCheckLogin
    @GetMapping("/bdQsTreeList")
    public R<List<BdTreeVo>> bdQsTreeList(BaseBsBdBo bo) {
        return iBaseBsBdService.bdQsTreeList(bo);
    }

    /**
     * 查询项目、标段、站点树列表
     */
    @SaCheckLogin
    @GetMapping("/projectTreeList")
    public R<List<BdTreeVo>> projectTreeList() {
        return iBaseBsBdService.projectTreeList();
    }

    /**
     * 查询带站点标段的分部分项树
     */
    @SaCheckLogin
    @GetMapping("/fbfxTreelist")
    public R<List<BdTreeVo>> getTreelist(BaseBsBdBo bo) {
        return R.ok(iBaseBsBdService.fbfxTreelist(bo));
    }

    /**
     * 根据标段id查询标段、分部分项 树
     */
    @SaCheckLogin
    @GetMapping("/zdFbfxTreelist")
    public R<List<BdTreeVo>> getZdFbfxTreelist(BaseBsBdBo bo) {
        return R.ok(iBaseBsBdService.getZdFbfxTreelist(bo));
    }

    /**
     * 查询标段列表不分页
     */
    @SaCheckLogin
    @GetMapping("/getList")
    public R<List<BaseBsBdVo>> getList(BaseBsBdBo bo) {
        return iBaseBsBdService.getList(bo);
    }

    /**
     * 查询当前用户下项目列表
     */
    @SaCheckLogin
    @GetMapping("/projectList")
    public R<List<BaseBsProject>> projectList() {
        return R.ok(iBaseBsBdService.projectList());
    }

    /**
     * 导出标段列表
     */
    @SaCheckLogin
    @Log(title = "标段", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseBsBdBo bo, HttpServletResponse response) {
        List<BaseBsBdVo> list = iBaseBsBdService.queryList(bo);
        ExcelUtil.exportExcel(list, "标段", BaseBsBdVo.class, response);
    }

    /**
     * 获取标段详细信息
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/{id}")
    public R<BaseBsBdVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBaseBsBdService.queryById(id));
    }

    /**
     * 根据站点id获取详细信息，工序报验用；
     *
     * @param id 主键
     */
    @SaCheckLogin
    @GetMapping("/getInfo/{id}")
    public R<BaseBsBdVo> getInfoById(@PathVariable Long id) {
        return R.ok(iBaseBsBdService.getInfoById(id));
    }

    /**
     * 新增标段
     */
    @SaCheckLogin
    @Log(title = "标段", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseBsBdBo bo) {
        return toAjax(iBaseBsBdService.insertByBo(bo));
    }

    /**
     * 修改标段
     */
    @SaCheckLogin
    @Log(title = "标段", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseBsBdBo bo) {
        return toAjax(iBaseBsBdService.updateByBo(bo));
    }

    /**
     * 删除标段
     *
     * @param ids 主键串
     */
    @SaCheckLogin
    @Log(title = "标段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBaseBsBdService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
